package com.heima.behavior.service.impl;

import com.heima.behavior.dtos.CollectionBehaviorDto;
import com.heima.behavior.pojos.ApCollectionBehavior;
import com.heima.behavior.service.ApCollectionBehaviorService;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.threadLocal.ApUserThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class ApCollectionBehaviorServiceImpl implements ApCollectionBehaviorService {


    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private MongoTemplate mongoTemplate;


    /**
     * 用户收藏行为操作
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult collection(CollectionBehaviorDto dto) {
        //1.参数效验
        if (dto.getEntryId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"该文章不存在");
        }

        //2.判断是否是登录用户还是游客身份
           //先判断用户是否为空,如果为空直接终止
        ApUser apUser = ApUserThreadLocalUtil.getApUser();
        if (apUser==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        Integer userId = apUser.getId();
        if (userId==0){
            //3.如果是游客则终止程序
            return ResponseResult.errorResult(AppHttpCodeEnum.NO_OPERATOR_AUTH,"游客身份,请登录以后再来进行操作");
        }

        //4.判断当前用户是否收藏过该文章
        String collectionKey="USER_COLLECTION_" +dto.getEntryId();
        Boolean flag = redisTemplate.opsForHash().hasKey(collectionKey, userId.toString());

        //5.如果收藏过,且正在执行的是收藏,则终止
        if (flag && dto.getOperation()==0){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"您已执行过收藏操作");
        }

        //6.如果收藏过,且正在执行的是取消收藏,则删除redis和mongo
        if (flag && dto.getOperation()==1){
            redisTemplate.opsForHash().delete(collectionKey,userId.toString());
            mongoTemplate.remove(Query.query(Criteria.where("userId").is(userId).and("entryId").is(dto.getEntryId())),ApCollectionBehavior.class);
        }

        //创建实体类对象
        ApCollectionBehavior apCollectionBehavior = getApCollectionBehavior(dto, userId);

        //7.如果没有收藏过,且正在执行的是文章收藏或者动态收藏,则新增redis和mongo
        if (flag && dto.getOperation()==0 && dto.getType()==0){
            redisTemplate.opsForHash().put(collectionKey,userId.toString(),"1");
            apCollectionBehavior.setType((short) 0);
            mongoTemplate.save(apCollectionBehavior);
        } else
        if (flag && dto.getOperation()==0 && dto.getType()==1){
            redisTemplate.opsForHash().put(collectionKey,userId.toString(),"1");
            apCollectionBehavior.setType((short) 1);
            mongoTemplate.save(apCollectionBehavior);
        }

        //9.如果没有收藏过,且正在执行的是取消收藏,则终止
        if (flag && dto.getOperation()==1){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"您已执行过取消收藏操作");
        }

        //10.返回数据
        return ResponseResult.okResult("收藏|取消收藏成功");
    }

    private ApCollectionBehavior getApCollectionBehavior(CollectionBehaviorDto dto, Integer userId) {
        ApCollectionBehavior apCollectionBehavior = new ApCollectionBehavior();
        apCollectionBehavior.setUserId(Long.valueOf(userId));
        apCollectionBehavior.setEntryId(dto.getEntryId());
        apCollectionBehavior.setCollectionTime(dto.getCreateTime());
        return apCollectionBehavior;
    }
}
